﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Telerik.Workflow.WebControls;
using System.Web.UI.WebControls;
using Telerik.Workflow;
using System.Web.Security;
using System.Net.Mail;
using Telerik.Blogs;
using Telerik.Cms.Web.UI;
using Telerik;
using Telerik.Cms.Engine.Security;
using Telerik.Cms.Engine;

namespace SelArom.Net.Sitefinity.Templates
{
	/// <summary>
	/// UserControl base for the BlogsItemPreview external template to send notification emails for content status change
	/// </summary>
	public class PostPreview : ContentItemPreviewBase
	{
		BlogManager blogMgr;
		protected override void OnLoad(EventArgs e)
		{
			// initialize generic manager and local events manager
			DefaultProvider = BlogManager.DefaultContentProvider;
			mgr = new ContentManager(DefaultProvider);

			// copy of manager for provider meta keys since NewsManager does not inherit from ContentManager
			blogMgr = new BlogManager(DefaultProvider);

			// initialize everything else (including content item data)
			base.OnLoad(e);
		}

		protected override string ForApprovalMessage()
		{
			var sb = new StringBuilder();
			// create email body
			sb.AppendFormat("{0} has submitted an blog post for approval!\n\nhttp://{1}/Sitefinity/Admin/Modules.aspx?module=Blogs\n\n", usr.UserName, Request.Url.Host);
			sb.Append("\nPlease REVIEW the entry and correct any issues before approving for publication!");

			// add event details if found
			if (contentItem != null)
			{
				sb.Append("Blog Post Details:\n\n");

				// ensure a title
				var title = contentItem.GetMetaData("Title");
				if (title == null) title = contentItem.GetMetaData("Name");
				if (title == null) title = "N/A";

				sb.AppendFormat("Title: {0}\nPublication Date: {1:MMM d, yyyy hh:mm tt}\n", title, contentItem.GetMetaData(blogMgr.PublicationDateField));
			}
			return sb.ToString();
		}

		protected override string ApprovedMessage()
		{
			var sb = new StringBuilder();
			// create email body
			sb.AppendFormat("{0} has approved an blog post for publication!\n\nhttp://{1}/Sitefinity/Admin/Modules.aspx?module=Blogs\n\n", usr.UserName, Request.Url.Host);

			// add event details if found
			if (contentItem != null)
			{
				sb.Append("Blog Post Details:\n\n");

				// ensure a title
				var title = contentItem.GetMetaData("Title");
				if (title == null) title = contentItem.GetMetaData("Name");
				if (title == null) title = "N/A";

				sb.AppendFormat("Title: {0}\nPublication Date: {1:MMM d, yyyy hh:mm tt}\n", title, contentItem.GetMetaData(blogMgr.PublicationDateField));
			}
			return sb.ToString();
		}
	}
}